@@ -6,7 +6,7 @@ class AgentsController < ApplicationController |
||
6 | 6 |
def index |
7 | 7 |
set_table_sort sorts: %w[name created_at last_check_at last_event_at last_receive_at], default: { created_at: :desc } |
8 | 8 |
|
9 |
- @agents = current_user.agents.preload(:scenarios, :controllers, :links_as_receiver, :links_as_source, :control_links_as_controller).reorder(table_sort).page(params[:page]) |
|
9 |
+ @agents = current_user.agents.preload(:scenarios, :controllers).reorder(table_sort).page(params[:page]) |
|
10 | 10 |
|
11 | 11 |
if show_only_enabled_agents? |
12 | 12 |
@agents = @agents.where(disabled: false) |
@@ -26,7 +26,7 @@ class ScenariosController < ApplicationController |
||
26 | 26 |
@scenario = current_user.scenarios.find(params[:id]) |
27 | 27 |
|
28 | 28 |
set_table_sort sorts: %w[name last_check_at last_event_at last_receive_at], default: { name: :asc } |
29 |
- @agents = @scenario.agents.preload(:scenarios, :controllers, :links_as_receiver, :links_as_source, :control_links_as_controller).reorder(table_sort).page(params[:page]) |
|
29 |
+ @agents = @scenario.agents.preload(:scenarios, :controllers).reorder(table_sort).page(params[:page]) |
|
30 | 30 |
|
31 | 31 |
respond_to do |format| |
32 | 32 |
format.html |
@@ -58,10 +58,10 @@ module AgentHelper |
||
58 | 58 |
end |
59 | 59 |
end |
60 | 60 |
|
61 |
- def agent_type_icon(agent) |
|
62 |
- receiver_count = agent.links_as_receiver.length |
|
63 |
- control_count = agent.control_links_as_controller.length |
|
64 |
- source_count = agent.links_as_source.length |
|
61 |
+ def agent_type_icon(agent, agents) |
|
62 |
+ receiver_count = links_counter_cache(agents)[:links_as_receiver][agent.id] || 0 |
|
63 |
+ control_count = links_counter_cache(agents)[:control_links_as_controller][agent.id] || 0 |
|
64 |
+ source_count = links_counter_cache(agents)[:links_as_source][agent.id] || 0 |
|
65 | 65 |
|
66 | 66 |
if control_count > 0 && receiver_count > 0 |
67 | 67 |
content_tag('span') do |
@@ -81,4 +81,22 @@ module AgentHelper |
||
81 | 81 |
icon_tag('glyphicon-unchecked') |
82 | 82 |
end |
83 | 83 |
end |
84 |
+ |
|
85 |
+ private |
|
86 |
+ |
|
87 |
+ def links_counter_cache(agents) |
|
88 |
+ @counter_cache ||= {} |
|
89 |
+ @counter_cache[agents.__id__] ||= {}.tap do |cache| |
|
90 |
+ agent_ids = agents.map(&:id) |
|
91 |
+ cache[:links_as_receiver] = Hash[Link.where(receiver_id: agent_ids) |
|
92 |
+ .group(:receiver_id) |
|
93 |
+ .pluck('receiver_id', 'count(receiver_id) as id')] |
|
94 |
+ cache[:links_as_source] = Hash[Link.where(source_id: agent_ids) |
|
95 |
+ .group(:source_id) |
|
96 |
+ .pluck('source_id', 'count(source_id) as id')] |
|
97 |
+ cache[:control_links_as_controller] = Hash[ControlLink.where(controller_id: agent_ids) |
|
98 |
+ .group(:controller_id) |
|
99 |
+ .pluck('controller_id', 'count(controller_id) as id')] |
|
100 |
+ end |
|
101 |
+ end |
|
84 | 102 |
end |
@@ -16,7 +16,7 @@ |
||
16 | 16 |
<% @agents.each do |agent| %> |
17 | 17 |
<tr> |
18 | 18 |
<td class='<%= "agent-unavailable" if agent.unavailable? %>'> |
19 |
- <%= agent_type_icon(agent) %> |
|
19 |
+ <%= agent_type_icon(agent, @agents) %> |
|
20 | 20 |
</td> |
21 | 21 |
<td class='<%= "agent-unavailable" if agent.unavailable? %>'> |
22 | 22 |
<%= link_to agent.name, agent_path(agent, return: (defined?(return_to) && return_to) || request.path) %> |